package cn.hncu.booksManagementSystem.in.dao.impl;

import java.util.ArrayList;
import java.util.Collection;

import cn.hncu.booksManagementSystem.common.enums.XxxIdEnum;
import cn.hncu.booksManagementSystem.in.dao.dao.InMainDAO;
import cn.hncu.booksManagementSystem.in.vo.InMainModel;
import cn.hncu.booksManagementSystem.in.vo.InMainQueryModel;
import cn.hncu.booksManagementSystem.utils.FileIoUtil;
import cn.hncu.booksManagementSystem.utils.GeneratorXxxID_Util;
/**
 * CreateTime: 2018年4月20日 下午11:09:52	
 * @author 宋进宇  Email:447441478@qq.com
 * Description:
 *	InMainDAO实现类
 */
public class InMainDAO_FileImpl implements InMainDAO {
	private final String FILE_NAME = "./data/inMain.info";
	
	@Override
	public boolean add(InMainModel inMain) {
		if (inMain==null) {
			return false;
		}
		Collection<InMainModel> col = FileIoUtil.readFromDataBase(FILE_NAME);
		for (InMainModel model : col) {
			if (model.equals(inMain)) {
				return false;
			}
		}
		boolean boo = col.add(inMain);
		boolean boo2 = FileIoUtil.write2DataBase(col, FILE_NAME);
		if (boo&&boo2) {
			GeneratorXxxID_Util.updateXxxId(XxxIdEnum.IN_MAIN);
			return true;
		}
		return false;
	}

	@Override
	public boolean detelte(String id) {
		if (id==null) {
			return false;
		}
		Collection<InMainModel> col = FileIoUtil.readFromDataBase(FILE_NAME);
		for (InMainModel model : col) {
			if (model.getId().equals(id)) {
				return col.remove(model)&&FileIoUtil.write2DataBase(col, FILE_NAME);
			}
		}
		return false;
	}

	@Override
	public boolean update(InMainModel inMain) {
		if (inMain==null) {
			return false;
		}
		Collection<InMainModel> col = FileIoUtil.readFromDataBase(FILE_NAME);
		ArrayList<InMainModel> list = (ArrayList<InMainModel>) col;
		for (int i = 0; i < list.size(); i++) {
			if(list.get(i).equals(inMain)) {
				return list.set(i, inMain)!=null&&FileIoUtil.write2DataBase(list, FILE_NAME);
				
			}
		}
		return false;
	}

	@Override
	public InMainModel getInMainInfoById(String id) {
		if (id==null) {
			return null;
		}
		Collection<InMainModel> col = FileIoUtil.readFromDataBase(FILE_NAME);
		for (InMainModel model : col) {
			if (model.getId().equals(id)) {
				return model;
			}
		}
		return null;
	}

	@Override
	public Collection<InMainModel> getAll() {
		return FileIoUtil.readFromDataBase(FILE_NAME);
	}

	@Override
	public Collection<InMainModel> getInMainsByCondition(InMainQueryModel imqm) {
		Collection<InMainModel> col = getAll();
		//如果查询条件为null，直接返回所有记录
		if (imqm==null) {
			return col;
		}
		Collection<InMainModel> resCol = new ArrayList<InMainModel>();
		for (InMainModel main : col) {
			//卫条件1 查id
			//判断查询条件是否有效
			if(imqm.getId()!=null &&!"".equals(imqm.getId())) {
				//不相等跳过
				if (!main.getId().equals(imqm.getId())) {
					continue;
				}
			}
			
			//卫条件2 查时间-范围查询：小于小的，大于大的
			//判断查询条件是否有效
			if (imqm.getInDate()>0) {
				//小于最小时间跳过
				if (main.getInDate()<imqm.getInDate()) {
					continue;
				}
			}
			//判断查询条件是否有效
			if (imqm.getInDate2()>0) {
				//大于最大时间跳过
				if (main.getInDate()>imqm.getInDate2()) {
					continue;
				}
			}
			
			//卫条件3 查进货人id
			//判断查询条件是否有效
			if (imqm.getInUserId()!=null && imqm.getInUserId().trim().length()>0) {
				//如果不相等跳过
				if (!main.getInUserId().trim().equals(imqm.getInUserId().trim())) {
					continue;
				}
			}
			//能到这了说明该记录符合条件
			resCol.add(main);
		}
		return resCol;
	}

}
